package com.villaday.idf.adapter2;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.adapter.DynamicListAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.foldtext.FoldTextView;
import com.villaday.idf.model.MaterialRecommendEntity;
import com.villaday.idf.model.Member_collect_listEntity;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import byc.imagewatcher.MessagePicturesLayout;

/**
 * 首页动态列表
 */

public class CollectListAdapter2 extends AbsRecyclerViewAdapter<Member_collect_listEntity.BodyBean.ListBean> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private MessagePicturesLayout.Callback mCallback;
    private final String roleType;
    private final SimpleDateFormat format;
    private final SimpleDateFormat formatNew;

    public CollectListAdapter2(Context context, OnAdapterCallbackListener onAdapterCallbackListener, View.OnClickListener onClickListener) {
        super(context, R.layout.item_dynamic_home0, /*R.layout.item_dynamic_1, R.layout.item_dynamic_2,*/ R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.onClickListener = onClickListener;

        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(context);
        roleType = roleInfo.getMember().getType();

        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatNew = new SimpleDateFormat("yyyy/MM/dd");
    }

    public CollectListAdapter2 setPictureClickCallback(MessagePicturesLayout.Callback callback) {
        mCallback = callback;
        return this;
    }

    @Override
    public int getItemType(Member_collect_listEntity.BodyBean.ListBean d) {
        return d.getMyType();
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, Member_collect_listEntity.BodyBean.ListBean d, int position) {
        switch (d.getMyType()) {
            case 0: {//超值特卖
                final TagFlowLayout idFlowlayout = (TagFlowLayout) holder.getView(R.id.id_flowlayout);
                idFlowlayout.setAdapter(new TagAdapter<String>(d.getTags()) {
                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.item_dynamic_tv_2v,
                                idFlowlayout, false);
                        tv.setText("#"+s);
                        return tv;
                    }
                });

                FoldTextView ftv = (FoldTextView) holder.getView(R.id.ftv);
                ftv.isExpand=false;
                ftv.setText(d.getAdWord());


                MessagePicturesLayout lPictures = (MessagePicturesLayout) holder.getView(R.id.l_pictures);
                lPictures.setCallback(mCallback);
                if (d.getMaterialType() == 1) {//为海报
                    List<String> url = new ArrayList<>();
                    url.add(d.getPoster());
                    lPictures.set(url, url);

                } else {
                    lPictures.set(d.getPicList(), d.getPicList());

                }


                try {
                    holder.bindSimpleDraweeView(R.id.sdv_head, d.getMerchant().getHeadImageURL())
                            .bindTextView(R.id.tv_name, d.getMerchant().getNickName())
                            .bindTextView(R.id.tv_date, formatNew.format(format.parse(d.getCreateTime())))
                            .setViewSelected(R.id.img_collect, d.isFavorite())

                            .setViewVisible(R.id.tv_lookShop, d.getVisitProduct()==0? View.GONE : View.VISIBLE)
                            .setViewVisible(R.id.tv_allFodder0, d.getSaveMaterial()==0? View.GONE : View.VISIBLE)
                            .setViewVisible(R.id.tv_share0, d.getForward()==0? View.GONE : View.VISIBLE)

                            .setClickListenerAndTag(R.id.img_collect, onClickListener, position)
                            .setClickListenerAndTag(R.id.tv_lookShop, onClickListener, position)
                            .setClickListenerAndTag(R.id.tv_allFodder0, onClickListener, position)
                            .setClickListenerAndTag(R.id.tv_share0, onClickListener, position);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                View view = holder.getView(R.id.tv_lookShop);
                if (TextUtils.isEmpty(d.getHouseId())) {
                    view.setVisibility(View.GONE);
                }

            }
            break;

            case 1: {
                onAdapterCallbackListener.onCallback();
            }
            break;


        }
    }
}
